# This is the extraction process DSL file.
#  Here the extraction can be done in both CSV as well as Fixed width.
class Extraction 
	
	
	
	def self.method_missing(method_name,arg1)
        $tab_array = arg1
        extraction_in_process = Extraction.new
        return extraction_in_process
	end	
	
	
	
	def from connection
        table_counter = 1
        tables_required = []
        all_tables = connection.tables
        if $tab_array.respond_to?"split"
            all_tables = get_columns_from_positions($tab_array,all_tables)
            string_param = true
        end
        for table in all_tables
            if $tab_array.include?table_counter
                tables_required << table
            elsif string_param
                tables_required << table	
            end
            table_counter = table_counter + 1
        end	
        extract_tables(tables_required,connection)
	end		
	
	
	
	def self.all_tables_from connection
        all_tables = connection.tables
        table_list = []
        if !all_tables.nil?
              for table in all_tables
                  table_list << table
              end
              extract_tables(table_list,connection)
        else
              puts "There are no tables under the given database"  
        end    
	end	
	
	
	
	# This method takes an array which will be the table names which you want to extract
	# In case all tables need to be extracted then option in :all
	def self.extract_to_csv(connection,table_hash)
        if connection == nil
              puts "You have not established the connection with the database..Please invoke the connect method of 
              the Connection class."
        else	
              table_list = table_hash[:table_names]
              if table_list == :all
                    table_list = get_all_table_list(connection)
                    puts "All tables are being extracted..." 
              else
                    puts "Following tables are being extracted..."
              end	
              extract_tables(table_list,connection)
        end
	end	
	
	
	
	# This is the sister method of the extract_to_csv.
	# As the name suggests the data gets extracted into a text file with a fixed width format
	# The additional width parameter is the width user has to specify.
	def self.extract_to_fixed_width(connection,width,&b)
        table_list = b.call
        if table_list == :all
            puts "All tables are being extracted..." 
            table_list = get_all_table_list(connection)
        else
            puts "Following tables are being extracted..."
        end	
        extract_tables_to_fixed_width(table_list,connection,width)
	end

	
	# This method gets all the table names from the database
	# This method is not for use directly but will be accessed by multiple other methods.
  def self.get_all_table_list(connection)
        all_tables_list = connection.tables
        table_list = []
        for table in all_tables_list
              table_list << table
        end	
        return table_list
	end	

end 	